﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace dzs.Web.supply
{
    public partial class ListMonthSupplyPlanDetail : System.Web.UI.Page
    {
        Maticsoft.BLL.monthSupplayPlan sm = new Maticsoft.BLL.monthSupplayPlan();
        dzs.BLL.distribution dism = new BLL.distribution();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    if (!string.IsNullOrEmpty(Request["supplyPlanIdListDetail"]))
                    {
                        Bind(user_id, Request["supplyPlanIdListDetail"]);
                    }

                    
                }
                //Bind(11);
            }
        }

        private void Bind(int user_id,string supplyPlanId)
        {
            //DataSet ds = sm.GetList(" instoreCount <> outstoreCount and user_id= "+user_id);//已入库数量=已出库数量

            string str = "  supplyPlanUser= " + user_id + " and supplyPlanId=" + supplyPlanId; //+ " and isUpErp = 0 ";


            if (!string.IsNullOrEmpty(txt_material.Text))
            {
                str += " and material like '%" + txt_material.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(txt_projectName.Text))
            {
                str += " and projectName like '%" + txt_projectName.Text + "%' ";
            }
         

            DataSet ds = sm.GetListInnums(str);


            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();

        }

    

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                if (!string.IsNullOrEmpty(Request["supplyPlanIdListDetail"]))
                {
                    Bind(user_id, Request["supplyPlanIdListDetail"]);
                }
            }

        }

        protected void btn_reload_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                if (!string.IsNullOrEmpty(Request["supplyPlanIdListDetail"]))
                {
                    Bind(user_id, Request["supplyPlanIdListDetail"]);
                }
            }
        }


        protected void btn_sel_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                if (!string.IsNullOrEmpty(Request["supplyPlanIdListDetail"]))
                {
                    Bind(user_id, Request["supplyPlanIdListDetail"]);
                }
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl_ghq = e.Row.FindControl("lbl_ghq") as Label;
                Label lbl_gyny = e.Row.FindControl("lbl_gyny") as Label;
                Label lbl_warn = e.Row.FindControl("lbl_warn") as Label;
                Label lbl_thisMonthNums = e.Row.FindControl("lbl_thisMonthNums") as Label;
                Label lbl_innums = e.Row.FindControl("lbl_innums") as Label;
                Label lbl_erpIsUp = e.Row.FindControl("lbl_erpIsUp") as Label;
                Label lbl_monthSupplyPlanId = e.Row.FindControl("lbl_monthSupplyPlanId") as Label;

                LinkButton lbn_erp = e.Row.FindControl("lbn_erp") as LinkButton;
                LinkButton lbn_del = e.Row.FindControl("lbn_del") as LinkButton;

                DataSet ds_distribution = dism.GetList(" gid = " + lbl_monthSupplyPlanId.Text);
                if (ds_distribution.Tables[0].Rows.Count > 0)//如果有配送单则不允许删除月供计划
                {
                    lbn_del.Enabled = false;
                }
                else
                {
                    lbn_del.Enabled = true;
                }

                if (!string.IsNullOrEmpty(lbl_gyny.Text) && !string.IsNullOrEmpty(lbl_ghq.Text))
                {
                    DateTime ghq = DateTime.Parse(lbl_ghq.Text);// 供货期
                    DateTime gyny = DateTime.Parse(lbl_gyny.Text);//供应年月

                    List<dzs.Model.configinfo> cm = new dzs.BLL.configinfo().GetModelList("");//获取时间结算日期

                    if (cm.Count == 1)
                    {
                        dzs.Model.configinfo c = cm[0];

                        #region 供货期起始时间
                        DateTime ghqMonthStartTime;//供货期的起始时间

                        int ghqDay = ghq.Day;//供货期

                        if (ghqDay < c.ygyjs)//当前日期小于配置结算日期，则按照上月到这个月计算，大于配置结算日期，则按照这个月到下月计算
                        {
                            ghqMonthStartTime = DateTime.Parse(ghq.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + ""));
                        }
                        else
                        {
                            ghqMonthStartTime = DateTime.Parse(ghq.ToString("yyyy-MM-" + c.ygyqs + ""));

                        }
                        #endregion
                        #region 本月供应计划起始结束时间 以当前系统时间为基准
                        //int day = DateTime.Today.Day;

                        //DateTime monthEndTime;//本月供应计划结束时间
                        //DateTime monthStartTime;//本月供应计划起始时间

                        //if (day < c.ygyjs)
                        //{
                        //    monthStartTime = DateTime.Parse(DateTime.Today.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + ""));
                        //    monthEndTime = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-" + c.ygyjs + ""));
                        //}
                        //else
                        //{
                        //    monthStartTime = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-" + c.ygyqs + ""));
                        //    monthEndTime = DateTime.Parse(DateTime.Today.AddMonths(1).ToString("yyyy-MM-" + c.ygyjs + ""));
                        //}
                        #endregion
                        #region 本月供应计划起始结束时间 以供应年月为基准
                        int gynyDay = gyny.Day;

                        DateTime g_monthEndTime;//供应年月结束时间
                        DateTime g_monthStartTime;//供应年月计划起始时间

                        if (gynyDay < c.ygyjs)
                        {
                            g_monthStartTime = DateTime.Parse(gyny.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + ""));
                            g_monthEndTime = DateTime.Parse(gyny.ToString("yyyy-MM-" + c.ygyjs + ""));
                        }
                        else
                        {
                            g_monthStartTime = DateTime.Parse(gyny.ToString("yyyy-MM-" + c.ygyqs + ""));
                            g_monthEndTime = DateTime.Parse(gyny.AddMonths(1).ToString("yyyy-MM-" + c.ygyjs + ""));
                        }
                        #endregion

                        #region 当月计划数量及入库数量
                        decimal thisMonthNums = 0;//当月计划数量
                        decimal innums = 0;//入库数量

                        if (!string.IsNullOrEmpty(lbl_thisMonthNums.Text))
                        {
                            thisMonthNums = decimal.Parse(lbl_thisMonthNums.Text);
                        }
                        if (!string.IsNullOrEmpty(lbl_innums.Text))
                        {
                            innums = decimal.Parse(lbl_innums.Text);
                        }
                        #endregion

                        #region 计算早到货和晚到货
                        if (DateTime.Compare(gyny, ghqMonthStartTime) < 0)//供应年月小于供货期起始时间 早到货
                        {
                            #region 将提前到货的标示注释掉了，为了以后早到货计算，请不要删除！！2014-10-31
                            //if (innums == 0)//入库数量为0
                            //{
                            //    lbn_erp.Visible = false;
                            //    lbl_warn.Visible = true;
                            //    lbl_warn.Text = "计划提前到货，但尚未入库";
                            //    lbl_warn.ForeColor = System.Drawing.Color.Green;//颜色标绿
                            //}
                            //else
                            //{
                            //    lbl_warn.Text = "提前到货";
                            //    lbl_warn.ForeColor = System.Drawing.Color.Green;//颜色标绿
                            //    lbn_erp.Visible = true;
                            //    if (innums == thisMonthNums)
                            //    {
                            //        lbn_erp.Enabled = true;
                            //    }
                            //    else
                            //    {
                            //        lbn_erp.Enabled = false;
                            //    }

                            //    lbn_erp.ForeColor = System.Drawing.Color.Green;//颜色标绿
                            //}

                            //if (lbl_erpIsUp.Text == "False")
                            //{
                            //    lbl_erpIsUp.Visible = true;
                            //    lbl_erpIsUp.Text = "未上传ERP";
                            //    lbl_erpIsUp.ForeColor = System.Drawing.Color.Green;//颜色标绿
                            //}
                            //else
                            //{
                            //    lbl_erpIsUp.Visible = true;
                            //    lbl_erpIsUp.Text = "已上传ERP";
                            //    lbl_erpIsUp.ForeColor = System.Drawing.Color.Black ;

                            //    lbl_warn.Text = "提前到货";
                            //    lbl_warn.ForeColor = System.Drawing.Color.Black;
                            //    lbn_erp.Visible = false;
                            //}
                            #endregion
                        }
                        else//正常到货、晚到货、计划没执行
                        {
                            if (DateTime.Compare(DateTime.Now,g_monthStartTime) < 0)//系统登录时间<月计划的起始时间（以供应年月为基准） 此计划还没执行
                            {
                                lbn_erp.Visible = false;
                                lbl_warn.Visible = true;
                                lbl_warn.Text = "此计划尚未执行";
                            }
                            else//供应年月小于配置的当月计划结束时间 此计划已经开始执行
                            {
                                //系统当前时间大于供应年月起始时间，且小于供应年月结束时间，即在当前供应月内
                                if (DateTime.Compare(DateTime.Now, g_monthEndTime) < 0 && DateTime.Compare(DateTime.Now, g_monthStartTime) > 0)
                                {
                                    if (innums < thisMonthNums)//入库数量小于当月计划数量
                                    {
                                        lbn_erp.Visible = false;
                                        lbl_warn.Visible = true;
                                        lbl_warn.Text = "本月计划正在执行，尚未完成";
                                    }
                                    else
                                    {
                                        lbn_erp.Visible = false;
                                        lbl_warn.Visible = true;
                                        lbl_warn.Text = "本月计划已完成";
                                    }

                                }
                                if (DateTime.Compare(DateTime.Now, g_monthEndTime) > 0)//系统登录时间大于月计划结束时间（以供应年月为基准）晚到货
                                {
                                    if (innums < thisMonthNums)//入库数量小于当月计划数量
                                    {
                                        lbn_erp.Visible = false;
                                        lbl_warn.Visible = true;
                                        lbl_warn.Text = "晚到货";
                                        lbl_warn.ForeColor = System.Drawing.Color.Red;//颜色标红
                                    }
                                    else
                                    {
                                        lbn_erp.Visible = false;
                                        lbl_warn.Visible = true;
                                        lbl_warn.Text = "本月计划已完成";
                                    }
                                }
                             
                              
                              
                            }
                        }
                        #endregion
                    }

                }

            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_del")
            {
                if (e.CommandArgument != null)
                {
                    string monthSupplyPlaneId = e.CommandArgument.ToString();

                    if (sm.Delete(int.Parse(monthSupplyPlaneId)))
                    {
                        if (Session["login"] != null)
                        {
                            DataSet ds = Session["login"] as DataSet;

                            int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                            if (!string.IsNullOrEmpty(Request["supplyPlanIdListDetail"]))
                            {
                                Bind(user_id, Request["supplyPlanIdListDetail"]);
                            }
                        }
                    }
                }
            }
            if (e.CommandName == "lbn_erp")
            {
                if (e.CommandArgument != null)
                {
                    GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                    Label lbl_supplyPlanId = gvr.FindControl("lbl_supplyPlanId") as Label;

                    Maticsoft.BLL.supplyPlan spm = new Maticsoft.BLL.supplyPlan();
                    Maticsoft.Model.supplyPlan sp = spm.GetModel(int.Parse(lbl_supplyPlanId.Text));

                    /*更新供应计划的早到货情况为false，由于早到货的情况在月供应计划详情里肯定只有一条记录
                      所以“上传Erp”按钮可以直接更改总计划的提前到货状态字段 isEarly 为false，当上传Erp后，此次计划彻底完成
                     不会再显示了*/
                    sp.isEarly = false;
                    spm.Update(sp);
                    
                    Maticsoft.Model.monthSupplayPlan m = sm.GetModel(int.Parse(e.CommandArgument.ToString()));

                    m.isUpErp = true;

                    if (sm.Update(m))
                    {
                        if (Session["login"] != null)
                        {
                            DataSet ds = Session["login"] as DataSet;

                            int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                            if (!string.IsNullOrEmpty(Request["supplyPlanIdListDetail"]))
                            {
                                Bind(user_id, Request["supplyPlanIdListDetail"]);
                            }
                        }
                    }

                 
                }
                
            }
        }
    }
}